<template>
	<view class="st-content">
		<view class="st-main">
			<view class="st-main-stlist">
				<view v-if="stList.length==0" style="width: 100%; text-align: center;">
					<text>暂无分享</text>
				</view>
				<uni-card v-if="stList.length>0" v-for="(item,index) in stList" :key="index">
								<image slot='cover' style="width: 100%; margin-top: 10rpx; border-radius: 10rpx;" :src="item.stCoverImage" @click="toStdetail(item.stId)" mode="widthFix"></image>
								<text class="uni-body">{{item.stTitle}}</text>
								<view slot="actions" class="card-actions st-card">
									<view class="card-actions-item">
										<uni-icons type="heart" size="18" color="#999"></uni-icons>
										<text class="card-actions-item-text" style="color: gray;">{{item.stZan}}</text>
									</view>
									<view class="card-actions-item">
										<uni-icons type="star" size="18" color="#999"></uni-icons>
										<text class="card-actions-item-text" style="color: gray;">{{item.stSc}}</text>
									</view>
									<view class="card-actions-item">
										<uni-icons type="trash" size="18" color="#999" @click="toOpenComfirm(index)"></uni-icons>
									</view>
								</view>
				</uni-card>
			</view>
			<view  v-if = "show" class="pop">
				<view style="width: 180rpx; margin:  0 auto; margin-left:140rpx;">
					<text>是否删除？</text>
				</view>
				<view style="width: 350rpx; margin: 0 auto; display: flex; justify-content: space-between; padding-left: 20rpx; margin-top: 20rpx;">
					<button @click="toCancel()" style="width: 150rpx; height: 60rpx; line-height: 60rpx; background-color: green; color: #ffffff;">取消</button>
					<button @click="toComfirm()" style="width: 150rpx; height: 60rpx; line-height: 60rpx;background-color: red; color: #ffffff;" >确认</button>
				</view>
			</view>
			<view class="st-footer">
				<p><text>没有更多内容了</text></p>
				
			</view>
			<view class="add-st" @click="toAddSt()">
				<image src="../../static/st-add.png" mode="" style="width: 96rpx; height: 96rpx;"></image>
			</view>
			
			
		</view>
			
		
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				current:0,
				show:false,
				scImages: [{
						url:"../../static/rb1.jpg"
					}, 
					{
						url:'../../static/rb2.jpg'
					}, 
					{
						url:'../../static/rb3.jpg'
					}],
				stList:[],
				delIndex:null,
			}
		},
		onLoad(){
			this.init()
		},
		onUnload(){
				var len = getCurrentPages().length
				console.log("len",len)
				if(len>=3){
					uni.navigateBack({
						delta:2
					})
				}
				
		},
		methods: {
			change(e){
				this.current = e.detail.current
			},
			toAddSt(){
				uni.navigateTo({
					url:"/pages/add-st/add-st"
				})
			},
			toStdetail(id){
				
				uni.navigateTo({
					url:"/pages/st-detail/st-detail?stid="+id
				})
			},
			toOpenComfirm(index){
				this.delIndex = index
				this.show = true
			    console.log(1)
			},
			toCancel(){
				this.show = false
				this.delIndex = null
			},
			toComfirm(){
				this.todel(this.delIndex)
				this.delIndex = null
				this.show = false
			},
			todel(index){
				//发送请求
				if(index==null){
					return 
				}else{
					console.log(index)
					var id = this.stList[index].stId
					
					this.request({
						url:"/strategy/del",
						method:'GET',
						data:{
							stId:id
						}
					}).then(res=>{
						if(res.code==='1'){
							this.stList = this.stList.filter(item=>{
								return item.stId != id
							})
							uni.showToast({
								title:'删除成功',
								icon:'success'
							})
						}else{
							uni.showToast({
								title:'删除失败，请稍后再试',
								icon:'error'
							})
						}
					}).catch(()=>{
						
						uni.showToast({
							title:'服务器异常',
							icon:'error'
						})
					})
				}
				
				//请求成功
				
			
				
				
			},
			async init(){
				await  this.request({
					url:"/strategy/myList",
					method:"GET"
				}).then(res=>{
					if(res.code==='1'){
						this.stList = res.data;
					}
					else{
						this.stList = null
					}
				}).catch(()=>{
					
					uni.showToast({
						title:'服务器异常',
						icon:'error'
					})
				})
			}
		}
	}
</script>

<style scoped>
	.pop {
		position: fixed;
		z-index: 1000;
		top:40%;
		left:25%;
		width: 400rpx;
		height: 150rpx;
		background-color: antiquewhite;
		border-radius:10rpx;
	}
.st-content{
	background-color: #f1f1f1f1;
}
.st-main {
	position: absolute;
	top:20rpx;
	width:100%;
	height: 700rpx;
	background-color: #ffffff;
	border-radius: 30rpx;
}
.st-main-header {
	margin-top: 20rpx;
}
.st-card {
	display: flex;
	justify-content: space-around;
}
.st-card view {
	margin-bottom: 10rpx;
}
.st-footer {
	text-align: center;
	margin-bottom: 20rpx;
	width: 100%;
}
.st-footer p{
	font-size: 25rpx;
	color: gray;
	width: 100%;
	margin-bottom: 20rpx;
}
.add-st {
	position: fixed;
	bottom: 100rpx;
	right: 0rpx;
}

</style>
